package cn.jekun.web.shop.action;

import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.LsLog;
import cn.jekun.component.core.bean.gen.LsLogType;
import cn.jekun.component.core.bean.gen.RsUser;
import cn.jekun.component.core.service.gen.LsLogTypeService;
import cn.jekun.web.shop.bean.TerminalLog;
import cn.jekun.web.shop.service.TerminalLogService;

/**
 * 终端日志记录
 * 
 * @author twj
 * 
 */
@SuppressWarnings("serial")
public class TerminalLogAction extends ShopBaseAction {
	
	private static final String AUTH_SHOP_TERMINAL_LOG="SHOP_TERMINAL_LOG";
	
	private String terminalId;
	private String type;
	private String userName;
	private String realName;
	private Date loginTime;

	@Resource(name=TerminalLogService.BEAN_NAME)
	TerminalLogService terminalLogService;
	@Resource(name=LsLogTypeService.BEAN_NAME)
	LsLogTypeService lsLogService;
	public String getTerminalId() {
		return terminalId;
	}

	public void setTerminalId(String terminalId) {
		this.terminalId = terminalId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public Date getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}
	/**
	 * 終端日志记录查询
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryTerminalLog(){
//		//判断是否有操作权限
//		this.checkRight(AUTH_SHOP_TERMINAL_LOG);
//		SqlUtis.convertBetweenValue(formMap, TerminalLog.CREATE_TIME);
//		formMap.put(RsTerminal.COL_TERMINAL_ID, terminalId);
//		formMap.put(LsLog.COL_LOG_TYPE_ID, type);
//		formMap.put(RsUser.COL_USER_NAME, userName);
//		formMap.put(RsUser.COL_REAL_NAME, realName);
//		formMap.put(ErpShop.COL_SHOP_ID, this.getSessionUser().getShopId());
//		SqlUtis.prepareSql(formMap
//				, SqlUtis.getSQL(formMap,"t."+RsTerminal.COL_TERMINAL_ID,Types.VARCHAR,SqlUtis.EQ),
//				SqlUtis.getSQL(formMap,"t."+LsLog.COL_LOG_TYPE_ID,Types.VARCHAR,SqlUtis.EQ),
//				SqlUtis.getSQL(formMap,"t."+RsUser.COL_USER_NAME,Types.VARCHAR,SqlUtis.EQ),
//				SqlUtis.getSQL(formMap,"t."+ErpShop.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),
//				SqlUtis.getSQL(formMap,"t."+RsUser.COL_REAL_NAME,Types.VARCHAR,SqlUtis.LIKE),
//				SqlUtis.getSQL(formMap,"t."+LsLog.COL_CREATE_TIME,Types.DATE)
//				);
//		this.resultList = this.terminalLogService.list(formMap, page);
//		return "/shop/terminallog/list.jsp";
		
		
		JSONObject jsonObject=new JSONObject();
		
		
		try {
			//判断是否有操作权限
			//this.checkRight(AUTH_SHOP_TERMINAL_LOG);
			String beginDate=request.getParameter("beginDate");
			String endDate=request.getParameter("endDate");
			String shopId=this.getSessionUser().getShopId();
			
			this.resultList = this.terminalLogService.queryTerminalLogList(beginDate,endDate,terminalId,type,
					userName,realName,shopId, page);
			
			JSONArray jsonArray = new JSONArray();
			
			HashMap<String, Object> hashMap;
			
			for(int i=0;i<this.resultList.size();i++){
				
			
				TerminalLog tempTerminalLog=(TerminalLog) resultList.get(i);
				
				hashMap=new HashMap<String, Object>();
				
				hashMap.put(TerminalLog.LOG_ID, tempTerminalLog.getLogId()==null?"":tempTerminalLog.getLogId());
				
				hashMap.put(TerminalLog.USER_NAME, tempTerminalLog.getUserName()==null?"":tempTerminalLog.getUserName());
				
				hashMap.put(TerminalLog.REAL_NAME, tempTerminalLog.getRealName()==null?"":tempTerminalLog.getRealName());
				
				hashMap.put(TerminalLog.TERMINAL_ID, tempTerminalLog.getTerminalId()==null?"":tempTerminalLog.getTerminalId());
				
				hashMap.put(TerminalLog.LOG_TYPE_NAME, tempTerminalLog.getLogTypeName()==null?"":tempTerminalLog.getLogTypeName());
				
				hashMap.put(TerminalLog.MEMO, tempTerminalLog.getMemo()==null?"":tempTerminalLog.getMemo());
				
				hashMap.put(TerminalLog.CREATE_TIME, tempTerminalLog.getCreateTime()==null?"":DateToDString(tempTerminalLog.getCreateTime()));
				
				jsonArray.add(hashMap);
				
			}
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");

			jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
			
			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
			
			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
			
			jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
			
			jsonObject.put("terminalLogsListData", jsonArray);
			
			
			
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		
		
		return this.writeJson(jsonObject);
		
		
		
		
		
	}
	/**
	 * 查询日志中所有的操作类型
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String querylogType(){
//		List<String> list = this.lsLogService.listBySql("select * from LS_LOG_TYPE");
//		JSONArray json = JSONArray.fromObject(list);
//		return this.writeJson(json);
		
		JSONObject jsonObject=new JSONObject();
		
		
		try {
			List list = this.lsLogService.listBySql("select * from LS_LOG_TYPE");
			
			JSONArray jsonArray = new JSONArray();
			
			HashMap<String, Object> hashMap;
			
			for(int i=0;i<list.size();i++){
				
				
				HashMap<String, Object> tempMap= (HashMap<String, Object>) list.get(i);
				
				hashMap=new HashMap<String, Object>();
				
				hashMap.put(LsLogType.COL_LOG_TYPE_ID, tempMap.get("LOG_TYPE_ID")==null?"":tempMap.get("LOG_TYPE_ID").toString());
				
				hashMap.put(LsLogType.COL_LOG_TYPE_NAME, tempMap.get("LOG_TYPE_NAME")==null?"":tempMap.get("LOG_TYPE_NAME").toString());
				
				jsonArray.add(hashMap);
				
				
			}
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");

			jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
			
			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
			
			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
			
			jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
			
			jsonObject.put("lsLogTypeListData", jsonArray);
			
			
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		
		return this.writeJson(jsonObject);
		
		
	}

}
